<template>
	<view class="wrap">
		<view class="wrap-header">
		</view>
		<view class="wrap-form">
			<view class="wrap-form-title">
				用户登录
			</view>
			<up-form>
				<up-form-item>
					<up-input v-model="phone" placeholder="手机号" shape="circle" prefixIcon="account"
						prefixIconStyle="font-size: 22px;color: #909399"></up-input>
				</up-form-item>
				<up-form-item>
					<up-input v-model="password" placeholder="密码" shape="circle" prefixIcon="lock" password
						prefixIconStyle="font-size: 22px;color: #909399"></up-input>
				</up-form-item>
				<up-form-item>
					<up-button type="error" shape="circle" @click="onSubmit" :disabled="checkInfo">立即登录</up-button>
				</up-form-item>
			</up-form>
			<view class="" style="margin-top: 20rpx;">
				<up-text text="忘记密码？" decoration="underline" size="small" align="center"></up-text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		computed,
		ref,
	} from 'vue';
	import loginApi from '../../api/login';
	import settingApi from '../../api/setting';
	// const phone = ref("13834040012")
	const phone = ref("13313515629")
	const password = ref("123456")
	const checkInfo = computed(() => !(phone.value && password.value))
	const onSubmit = async () => {
		await tmpLogin();
		// await getMyInfo();
		await appLogin();
		uni.reLaunch({
			url: "/pages/index/index"
		})
	}

	const appLogin = async () => {
		const reqObj = {
			m: phone.value,
			p: password.value,
			loginType: '5',
			deviceToken: '',
			lat: 37.54,
			lng: 112.33,
			version: '36',
			isThird: '1',
			thirdAccount: '',
			appPackageeName: '',
			app: '4',
		};
		const res = await settingApi.appLogin(reqObj);
		console.log(res);
		uni.setStorageSync("app", JSON.stringify(res.data));
	}
	const tmpLogin = async () => {
		console.log(phone.value, password.value);
		const res = await loginApi.getToken({
			client_id: "uhome.android",
			client_secret: "16f03e54ebb04bb4ac13fdfdaf51921f",
			grant_type: "password",
			username: encodeURIComponent(phone.value),
			password: encodeURIComponent(password.value),
			scope: "identity uhome.rke uhome.o2o"
		});
		console.log(res);
		let {
			access_token,
			expires_in,
			scope,
			token_type
		} = res;
		uni.setStorageSync("token", token_type + " " + access_token);
		uni.setStorageSync("expires_in", expires_in);
		uni.setStorageSync("scope", scope);
	};
	const getMyInfo = async () => {
		const res = await loginApi.getMyInfo();
		console.log(res);
		if (res.State) {
			uni.setStorageSync("user", JSON.stringify(res.Data));
		} else {
			console.log("获取用户信息失败")
		}
	};
</script>

<style lang="scss" scoped>
	$page-color: #ff819f;

	.wrap {
		height: 100vh;
		background-color: #fff;

		.wrap-header {
			height: 30vh;
			background-color: $page-color;
		}

		.wrap-form {
			margin: -150rpx 40rpx 0;
			padding: 40rpx 40rpx 0;
			background-color: #fff;
			border-radius: 40rpx;
			color: $page-color;

			.wrap-form-title {
				margin: 10rpx 0 40rpx;
				letter-spacing: 5rpx;
			}
		}
	}
</style>